package cn.csc.module.member.dal.mysql.browsinghistory;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.module.member.controller.app.browsinghistory.vo.AppProductBrowsingHistoryExportReqVO;
import cn.csc.module.member.controller.app.browsinghistory.vo.AppProductBrowsingHistoryPageReqVO;
import cn.csc.module.member.dal.dataobject.browsinghistory.ProductBrowsingHistoryDO;
import org.apache.ibatis.annotations.Mapper;

import java.math.BigInteger;
import java.util.List;

/**
 * 商品浏览记录 Mapper
 *
 * @author liuchuang
 */
@Mapper
public interface ProductBrowsingHistoryMapper extends BaseMapperX<ProductBrowsingHistoryDO> {

    default PageResult<ProductBrowsingHistoryDO> selectPage(AppProductBrowsingHistoryPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ProductBrowsingHistoryDO>()
                .eqIfPresent(ProductBrowsingHistoryDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ProductBrowsingHistoryDO::getSpuId, reqVO.getSpuId())
                .betweenIfPresent(ProductBrowsingHistoryDO::getViewTime, reqVO.getViewTime())
                .eqIfPresent(ProductBrowsingHistoryDO::getViewCount, reqVO.getViewCount())
                .betweenIfPresent(ProductBrowsingHistoryDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ProductBrowsingHistoryDO::getId));
    }

    default List<ProductBrowsingHistoryDO> selectList(AppProductBrowsingHistoryExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ProductBrowsingHistoryDO>()
                .eqIfPresent(ProductBrowsingHistoryDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ProductBrowsingHistoryDO::getSpuId, reqVO.getSpuId())
                .betweenIfPresent(ProductBrowsingHistoryDO::getViewTime, reqVO.getViewTime())
                .eqIfPresent(ProductBrowsingHistoryDO::getViewCount, reqVO.getViewCount())
                .betweenIfPresent(ProductBrowsingHistoryDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ProductBrowsingHistoryDO::getId));
    }


    default ProductBrowsingHistoryDO selectByUserIdAndSpuId(BigInteger userId, BigInteger spuId) {
        return selectOne(new LambdaQueryWrapperX<ProductBrowsingHistoryDO>()
                .eq(ProductBrowsingHistoryDO::getUserId, userId)
                .eq(ProductBrowsingHistoryDO::getSpuId, spuId)
        );
    }

    default List<ProductBrowsingHistoryDO> selectByUserId(BigInteger userId) {
        return selectList(new LambdaQueryWrapperX<ProductBrowsingHistoryDO>()
                .eq(ProductBrowsingHistoryDO::getUserId, userId)
                .last("limit 50")
        );
    }

    default List<ProductBrowsingHistoryDO> getSpuIdList() {
        return selectList(new LambdaQueryWrapperX<ProductBrowsingHistoryDO>()
                .orderByDesc(ProductBrowsingHistoryDO::getViewTime)
                .last("limit 100")
        );
    }



}
